package com.atguigu.ssyx.activity.controller;

import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.activity.ActivityRule;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-08-07 15:53 星期一
 * description:
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/activity/activityInfo")
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;


    /**
     * 获取活动分页列表
     * @return
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation(value = "获取活动分页列表")
    public Result index(
            @ApiParam(name = "page",value = "当前页面",required = true) 
            @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable("limit") Long limit
    ){
        IPage<ActivityInfo> pageMode = activityInfoService.selectPage(page,limit);
        return Result.ok(pageMode);
    }

    /**
     * 获取活动
     * @return
     */
    @ApiOperation(value = "获取活动")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id){
        ActivityInfo activityInfo = activityInfoService.getById(id);
        //封装ActivityInfo中setActivityTypeString属性
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    /**
     * 新增活动
     * @return
     */
    @ApiOperation(value = "新增活动")
    @PostMapping("/save")
    public Result save(@RequestBody ActivityInfo activityInfo){
        activityInfo.setCreateTime(new Date());
        activityInfoService.save(activityInfo);
        return Result.ok();
    }

    /**
     * 修改活动
     * @return
     */
    @ApiOperation(value = "修改活动")
    @PutMapping("/update")
    public Result remove(@RequestBody ActivityInfo activityInfo){
        activityInfo.setUpdateTime(new Date());
        activityInfoService.updateById(activityInfo);
        return Result.ok();
    }

    /**
     * 根据id删除活动
     * @return
     */
    @ApiOperation(value = "删除活动")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        activityInfoService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id列表批量删除活动
     * @return
     */
    @ApiOperation(value = "批量删除活动列表")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        activityInfoService.removeByIds(ids);
        return Result.ok();
    }


    /**
     * 根据活动id获取活动规则
     * @return
     */
    @ApiOperation(value = "获取活动规则")
    @GetMapping("/findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable("id") Long id){
        Map<String,Object> activityRuleList = activityInfoService.findActivityRuleList(id);
        return Result.ok(activityRuleList);
    }


    /**
     * 新增活动规则
     * @return
     */
    @ApiOperation(value = "新增活动规则")
    @PostMapping("/saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo){
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok();
    }

    /**
     * 根据关键字获取sku列表，提供给添加活动范围使用
     * @return
     */
    @ApiOperation(value = "根据关键字获取sku列表，提供给添加活动范围使用")
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable("keyword") String keyword){
        List<SkuInfo> skuInfoList = activityInfoService.findSkuInfoByKeyword(keyword); 
        return Result.ok(skuInfoList);
    }
}
